package com.johnny.xia.jsr;

import org.hibernate.validator.HibernateValidator;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;

public class Jsr308 {

    /**
     * 使用hibernate的注解来进行验证
     *
     */
    private static Validator validator = null;

    @BeforeClass
    public static void initValidator(){
        validator = Validation.byProvider(HibernateValidator.class).configure().failFast(true).buildValidatorFactory().getValidator();

//        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
//        validator = factory.getValidator();
    }

    @Test
    public void validatorTest(){
        Sku sku = new Sku();
        Set<ConstraintViolation<Sku>> constraintViolations = validator.validate(sku);
        System.out.println("constraintViolations.size():"+constraintViolations.size());
        System.out.println(constraintViolations.iterator().next().getMessage());
    }




}
